/*
 * Copyright 2015 William Wang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wxk.Bonus.Constant;

import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.animation.TranslateTransitionBuilder;
import javafx.application.Platform;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Duration;
import wxk.Bonus.Network.UnionLottoSync;
import wxk.Bonus.bean.ChinaDoubleBallBean;
import wxk.Bonus.utils.BonusLogger;
/**
 *
 * @author William Wang
 */
public final class GlobalConfig {
    static private Stage global_stage=null;
    static private Label statusbar=null;
    static GlobalConfig global =null;
    static HBox bottomstatusbar=null;

    static  Circle circle = new Circle(20, Color.CRIMSON);
    static boolean iscircleadded=false;
    static private TranslateTransition translateTransition;
 BonusLogger mylog = BonusLogger.getInstance();
public  FilteredList<ChinaDoubleBallBean> filterData= 
             new FilteredList<>(UnionLottoSync.datalist, p -> true);;

    public  void setBottomstatusbar(HBox bottomstatusbar) 
    {
        GlobalConfig.bottomstatusbar = bottomstatusbar;
    }
public void setGlobalStage(Stage sg)
{
    global_stage=sg;
}
 private GlobalConfig(){

 };
 
 public static GlobalConfig getInstance()
 {
  if(global==null)global=new GlobalConfig();
  return global;
 }
    public  void setStatusbar(Label statusbar) 
    {
        GlobalConfig.statusbar = statusbar;
    }
  
    public  void updateSystemStatusBar(String msg)
    {
     if(statusbar==null){
         System.out.println("status bar is null!Please check!");
         return;   }
      Platform.runLater(new Runnable(){
          @Override
          public void run() {
//        System.out.println("status bar ready for update!");              
            statusbar.setText(msg);
          }
      });
    }
    public void showWorking()
    {
     Platform.runLater(new Runnable(){

         @Override
         public void run() {
        
        Pane par=(Pane)global_stage.getScene().getRoot();
        if(par!=null && !iscircleadded)
        {
        par.getChildren().add(circle);
        iscircleadded=true;
        }else
        {
          System.out.println("Root Pane is null?Wired.");  
        }
         circle.setVisible(true);
        translateTransition.play();
        try
        {
        Thread.sleep(DBContant.SLEEP_COUNT);
        }catch(Exception e){}
         }
         
     });
    }
    public void stopWorking()
    {
     Platform.runLater(new Runnable(){

         @Override
         public void run() {
           translateTransition.stop();
        circle.setVisible(false);
         }
         
     });        

        
    }
    
  public void createTranslate()
  {
    mylog.log("createTranslate be called....");
        circle.setTranslateX(20);
        circle.setTranslateY(20);
        circle.setLayoutX(bottomstatusbar.localToScene(0.0, 0.0).getX());
        circle.setLayoutY(bottomstatusbar.localToScene(0.0, 0.0).getY());
mylog.log("bottomstatusbar.localToScene(0.0, 0.0).getX():"+bottomstatusbar.localToScene(0.0, 0.0).getX());
mylog.log("bottomstatusbar.localToScene(0.0, 0.0).getY():"+bottomstatusbar.localToScene(0.0, 0.0).getY());

        translateTransition = TranslateTransitionBuilder.create()
                .duration(Duration.seconds(4))
                .node(circle)
                .fromX(bottomstatusbar.getLayoutX())
                .toX(bottomstatusbar.getWidth())
                .cycleCount(Timeline.INDEFINITE)
                .autoReverse(true)
                .build();      
  }
}
